<template>
  <div class="login-container">
    <div style="width: 100%; padding: 10px 20px; font-size: 24px; background-color: rgba(0,0,0,.3); color: white; position: absolute; top: 0">后台管理系统</div>

    <div class="login-box">
        <div style="text-align: center; font-size: 20px; margin-bottom: 20px; color: #333">欢迎使用</div>
        <el-form :model="form" :rules="rules" ref="formRef">
          <el-form-item prop="username">
            <el-input :prefix-icon="User" size="large" v-model="form.username" placeholder="请输入账号"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input show-password :prefix-icon="Lock" size="large" v-model="form.password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item prop="role">
            <el-select v-model="form.role" style="width: 100%">
              <el-option label="管理员" value="ADMIN"></el-option>
              <el-option label="用户" value="USER"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <div style="display: flex; align-items: center">
              <div style="flex: 1">
                <el-input size="medium" v-model="code" placeholder="请输入验证码"></el-input>
              </div>
              <Identify @click="refreshCode" :identify-code="identifyCode"/>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button style="width: 100%; background-color: #333; border-color: #333; color: white" @click="login">登 录</el-button>
          </el-form-item>
          <div style="display: flex">
            <a style="flex:1;color:#1890ff;" href="/front/home">游客登录</a>
            <div style="text-align: right;color: black">
              还没有账号？请 <a href="/register">注册</a>
            </div>
          </div>
        </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import request from "@/utils/request.js";
import { User, Lock } from "@element-plus/icons-vue";

const router = useRouter();

const formRef = ref(null);
const form = reactive({ role: 'ADMIN' });
const code = ref('');
const identifyCode = ref('');
const identifyCodes = '3456789ABCDEFGHGKMNPQRSTUVWXY';
const rules = {
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ]
};

onMounted(() => {
  refreshCode();
});

const login = () => {
  if(code.value.toLowerCase() !== identifyCode.value.toLowerCase()){
    ElMessage.warning("验证码错误");
    refreshCode();
    code.value = '';
    return;
  }
  formRef.value.validate((valid) => {
    if (valid) {
      request.post('/login', form).then(res => {
        if (res.code === '200') {
          localStorage.setItem("xm-user", JSON.stringify(res.data));
          if (form.role === 'USER') {
            location.href = '/front/home'
          } else {
            location.href = '/manager/home'
          }
          ElMessage.success('登录成功');
        } else {
          ElMessage.error(res.msg);
        }
      })
    }
  })
};

const refreshCode = () => {
  identifyCode.value = '';
  makeCode(identifyCodes, 4);
};

const makeCode = (o, l) => {
  for (let i = 0; i < l; i++) {
    identifyCode.value += o[Math.floor(Math.random() * o.length)];
  }
};
</script>

<style scoped>
.login-container {
  height: 100vh;
  overflow: hidden;
  background-image: url("../assets/imgs/bg.jpg");
  background-size: 100% 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;
}
.login-box {
  width: 350px;
  padding: 30px;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  background-color: rgba(255, 255, 255, 0.5);
}
</style>
